{% extends "base.html" %}

{% block content %}
    <h2>添加积分记录</h2>
    <form method="POST">
        <div class="mb-3">
            <label for="account" class="form-label">账号</label>
             <input type="text" class="form-control" id="account" name="account"
                   value="{{ account_filter }}" required>
        </div>
        <div class="mb-3">
            <label for="date" class="form-label">日期</label>
            <!-- 设置默认值为当天日期 -->
            <input type="date" class="form-control" id="date" name="date"
                   value="{{ today_date }}" required>
        </div>
        <div class="mb-3">
            <label for="balance" class="form-label">余额积分</label>
            {% set balance = 4 if account_filter == 'xy' else 3 %}
            <input type="number" class="form-control" id="balance" name="balance"  value="{{balance}}" required>
            <!-- 新增的计算结果显示 -->
                <div class="alert alert-info mt-2" id="calculationResult">
                    <i class="bi bi-calculator"></i> 计算结果: 1024 * 8
                </div>
        </div>
        <div class="mb-3">
            <label for="transaction" class="form-label">交易量积分</label>
            <input type="number" class="form-control" id="transaction" name="transaction" value="15" required>
        </div>
        <div class="mb-3">
            <label for="consumption" class="form-label">消耗积分</label>
            <input type="number" class="form-control" id="consumption" name="consumption" value="0" required>
        </div>
        <div class="mb-3">
            <label for="remark" class="form-label">备注</label>
            <textarea class="form-control" id="remark" name="remark" rows="3"></textarea>
        </div>
        <button type="submit" class="btn btn-primary">提交</button>
        <a href="/" class="btn btn-secondary">取消</a>
    </form>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const transactionInput = document.getElementById('transaction');
            const calculationResult = document.getElementById('calculationResult');

            // 初始计算
            updateCalculation();

            // 监听交易量积分变化
            transactionInput.addEventListener('input', updateCalculation);

            function updateCalculation() {
                const n = parseInt(transactionInput.value);

                if (!isNaN(n) && n >= 0) {
                    // 计算 2的n次方除以4
                    const result = Math.pow(2, n) / 4;
                    // 计算倍数
                    const multiplier = result / 1024;

                    // 更新显示
                    calculationResult.innerHTML = `<i class="bi bi-calculator"></i> 计算结果: 1024 * ${multiplier}`;
                } else {
                    calculationResult.innerHTML = `<i class="bi bi-exclamation-triangle"></i> 请输入有效的交易量积分`;
                }
            }
        });
    </script>
{% endblock %}
